package com.itheima.controller;


import com.itheima.Utils.AliOSSUtils;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

//    @PostMapping("/upload")
//    public Result upload(String usernamme ,Integer age ,
//                         MultipartFile image
//                         ) throws Exception {
//        log.info("文件上传",usernamme,age,image);
//
//        //        获取文件原始名字
//        String originalFilename = image.getOriginalFilename();
//        //        构造文件名     uuid（统一、唯一的字符串）
////        首先获取文件名称后缀  然后截取字符串  然后组装一下uuid+后缀
//        int index = originalFilename.lastIndexOf(".");
//        String exname = originalFilename.substring(index);
//        String newFileName = UUID.randomUUID().toString() + exname;
//        log.info("新的文件名称",newFileName);
//        //        存文件
//        image.transferTo(new File("X:\\upload\\"+newFileName));
//
//        return Result.success();
//    }
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("文件上传oss:{}",image);
//        调用阿里云oss
        String url =  aliOSSUtils.upload(image);
        log.info("文件长传完成，文件访问的url:{}",url);
        return Result.success(url);
    }
}
